package com.liang.subject.infra.basic.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.subject.infra.basic.entity.SubjectLabel;
import com.liang.subject.infra.basic.entity.SubjectMapping;
import com.liang.subject.infra.basic.service.SubjectLabelService;
import com.liang.subject.infra.basic.service.SubjectMappingService;
import com.liang.subject.infra.basic.mapper.SubjectMappingMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 29018
 * @description 针对表【subject_mapping(题目分类关系表)】的数据库操作Service实现
 * @createDate 2024-10-11 13:34:24
 */
@Service
@RequiredArgsConstructor
public class SubjectMappingServiceImpl extends ServiceImpl<SubjectMappingMapper, SubjectMapping> implements SubjectMappingService {

    private final SubjectLabelService subjectLabelService;

    @Override
    public List<SubjectLabel> getLabelListByCategoryId(Long categoryId) {
        return getSubjectLabel(lambdaQuery().eq(SubjectMapping::getCategoryId, categoryId), categoryId);
    }

    @Override
    public List<SubjectLabel> getLabelListBySubjectId(Long subjectId) {
        return getSubjectLabel(lambdaQuery().eq(SubjectMapping::getSubjectId, subjectId), subjectId);
    }

    private List<SubjectLabel> getSubjectLabel(LambdaQueryChainWrapper<SubjectMapping> eq, Long val) {
        List<SubjectMapping> subjectMappingList = eq.list();
        if (CollectionUtils.isEmpty(subjectMappingList)) {
            return Collections.emptyList();
        }
        List<Long> labelIdList = subjectMappingList.stream()
                .map(SubjectMapping::getLabelId)
                .distinct()
                .collect(Collectors.toList());
        return subjectLabelService.listByIds(labelIdList);
    }
}




